package com.ilink.groundservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.PO.innovationPlaza.ProjectUser;
import com.ilink.groundservice.entity.VO.innovationPlaza.ProjectResponse;
import com.ilink.groundservice.entity.VO.innovationPlaza.ProjectdetailsIntroduceResponse;
import com.ilink.groundservice.entity.VO.innovationPlaza.ProjectdetailsMetaResponse;
import com.ilink.groundservice.service.InnovationPlaza.ProjectService;
import com.ilink.ilinkapi.dto.ground.project.ProjectMetadataRequest;
import com.ilink.ilinkapi.dto.ground.project.dataload.ProjectData678DTO;
import com.ilink.ilinkapi.dto.ground.project.dataload.ProjectDataDTO;
import com.ilink.ilinkcommon.domain.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.ilink.ilinkcommon.log.method.MethodLog;
@Slf4j
@RestController
@RequestMapping("/ground/innovationPlaza/project")
@RequiredArgsConstructor
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    /**
     * @author dzy
     * @return 最常使用的8个标签(List<InnovationTags>形式)
     */
    @MethodLog("最常使用的八个标签")
    @GetMapping("/getCommonProjectTags")
    public R getCommonDatasetTags(){
        try {
            return R.ok(projectService.getCommonTags());
        } catch(Exception e){
            return R.error("内部服务器错误：" + e);
        }
    }



    /**
     * @Author huangqing
     * @Description 分页返回项目展示数据
     * @Date 上午10:35 2024/9/18
     * @Param [request]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "分页返回项目展示数据")
    @PostMapping("/getALLProjects")
    public R getALLProjects(@RequestBody ProjectMetadataRequest request) {
        try {
            IPage<ProjectResponse> projectResponsePage = projectService.getALLProjects(request);

            if (projectResponsePage != null) {
                return R.ok("分页查询项目元数据", projectResponsePage);
            } else {
                return R.error("未查询到项目元数据");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author huangqing
     * @Description 获取项目的详细信息上面
     * @Date 上午10:37 2024/9/18
     * @Param
     * @return
     **/
    @MethodLog("获取项目详情信息")
    @GetMapping("/getProjectDetailsMeta")
    public R getProjectDetailsMeta(@RequestParam Long projectId) {
        try {
            // 1. 校验 projectId 是否有效
            if (projectId == null || projectId <= 0) {
                return R.error("无效的项目ID");
            }
            // 2. 调用服务层方法获取项目元数据
            ProjectdetailsMetaResponse projectDetailsMeta = projectService.getProjectDetailsMeta(projectId);
            // 3. 检查返回结果是否为空
            if (projectDetailsMeta == null) {
                return R.error("未找到相关项目元数据");
            }
            // 4. 返回成功响应
            return R.ok("查询成功", projectDetailsMeta);
        } catch (Exception e) {
            // 记录异常并返回错误响应
            log.error("获取项目元数据时发生错误：", e);
            return R.error("系统内部错误: " + e.getMessage());
        }
    }

    /**
     * @Author huangqing
     * @Description 获取项目的详细信息：项目介绍
     * @Date 上午10:37 2024/9/18
     * @Param
     * @return
     **/
    @MethodLog("获取项目的详细信息，项目介绍")
    @GetMapping("/getProjectDetailsIntroduce")
    public R getProjectDetails(@RequestParam Long projectId) {
        try {
            // 1. 校验 projectId 是否有效
            if (projectId == null || projectId <= 0) {
                return R.error("无效的项目ID");
            }

            // 2. 调用服务层方法获取项目介绍
            ProjectdetailsIntroduceResponse projectDetailsIntroduce = projectService.getProjectIntroduce(projectId);

            // 3. 检查返回结果是否为空
            if (projectDetailsIntroduce == null) {
                return R.error("未找到项目介绍");
            }

            // 4. 返回成功响应
            return R.ok("查询成功", projectDetailsIntroduce);
        } catch (Exception e) {
            // 记录异常并返回错误响应
            log.error("获取项目介绍时发生错误：", e);
            return R.error("系统内部错误: " + e.getMessage());
        }
    }
    /**
     * @Author huanqging
     * @Description 获取项目的详细信息：项目成员
     * @Date 上午10:38 2024/9/18
     * @Param
     * @return
     **/
    @MethodLog("获取项目的详情信息：项目成员")
    @GetMapping("/getProjectDetailsmembers")
    public R getProjectMembers(@RequestParam Long projectId) {
        try {
            // 1. 校验 projectId 是否有效
            if (projectId == null || projectId <= 0) {
                return R.error("无效的项目ID");
            }

            // 2. 调用服务层方法获取项目成员列表
            List<ProjectUser> projectMembers = projectService.getProjectMembers(projectId);

            // 3. 检查返回的项目成员列表是否为空
            if (projectMembers == null || projectMembers.isEmpty()) {
                // 若未找到项目成员，返回状态码为200的成功响应，只是提示信息表明未找到成员
                return R.ok("未找到项目成员", projectMembers);
            }

            // 4. 返回成功响应
            return R.ok("查询成功", projectMembers);
        } catch (Exception e) {
            // 记录异常并返回错误响应
            log.error("获取项目成员时发生错误：", e);
            return R.error("系统内部错误: " + e.getMessage());
        }
    }

    @PostMapping("/insert")
    public ResponseEntity<String> insertProject(@RequestBody ProjectDataDTO projectDataDTO) {
        projectService.insertProjectData(projectDataDTO);
        return ResponseEntity.ok("Project data inserted successfully");
    }

    @PostMapping("/insert/yanchuang678")
    public ResponseEntity<String> insertProject678(@RequestBody ProjectData678DTO projectData678DTO) {
        projectService.insertProjectData678(projectData678DTO);
        return ResponseEntity.ok("Project data inserted successfully");
    }

}
